package com.cx.zyx.modulebiz.news.config;

import io.lettuce.core.ReadFrom;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.time.Duration;
import java.util.HashSet;
import java.util.Set;

@Configuration
public class NewsWebConfig {
    @Bean
    public RedisTemplate<String,Long> redisTemplate(RedisConnectionFactory factory){
        RedisTemplate<String, Long> template = new RedisTemplate<>();
        template.setConnectionFactory(factory);
        template.setKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());
        template.afterPropertiesSet();
//        template.opsForValue().set("test", 123L);
        return template;
    }
    // --- 配置 RestTemplate for Load Balancing ---
//    @Bean
//    @LoadBalanced // 标记这个 RestTemplate Bean 具有负载均衡能力
//    public RestTemplate loadBalancedRestTemplate(RestTemplateBuilder builder) {
//        // 使用 RestTemplateBuilder 来构建 RestTemplate 实例
//        // 可以在这里添加其他通用配置，例如超时设置、拦截器等
//        return builder
//                .setConnectTimeout(java.time.Duration.ofSeconds(3)) // 连接超时
//                .setReadTimeout(java.time.Duration.ofSeconds(5))    // 读取超时
//                .build();
//    }
//
//    // 如果还需要一个不带负载均衡功能的 RestTemplate，可以再定义一个
//    @Bean("plainRestTemplate")
//    public RestTemplate plainRestTemplate(RestTemplateBuilder builder) {
//        return builder.build();
//    }

}
